package com.itaq.cheetah.serviceA.config.web;

import com.alibaba.ttl.TransmittableThreadLocal;
import com.itaq.cheetah.serviceA.model.entity.SsoClientDetail;

/**
 * @author cheetah
 * @date 2021/12/13
 */
public class AuthContextHolder {

    private final TransmittableThreadLocal<SsoClientDetail> threadLocal;

    /**
     * 静态内部类单例模式
     * 单例初使化
     */
    private static final AuthContextHolder INSTANCE = new AuthContextHolder();


    /**
     * 构造函数
     */
    private AuthContextHolder() {
        this.threadLocal = new TransmittableThreadLocal<>();
    }

    /**
     * 创建实例
     *
     * @return
     */
    public static AuthContextHolder getInstance() {
        return AuthContextHolder.INSTANCE;
    }

    /**
     * 用户上下文中放入信息
     *
     * @param jsonObject
     */
    public void setContext(SsoClientDetail jsonObject) {
        threadLocal.set(jsonObject);
    }

    /**
     * 获取上下文中的信息
     *
     * @return
     */
    public SsoClientDetail getContext() {
        return threadLocal.get();
    }

    /**
     * 清空上下文
     */
    public void clear() {
        threadLocal.remove();
    }


    /**
     * 获得上下文用户信息
     *
     * @return
     */
    public SsoClientDetail authUser() {
        return getContext();
    }

}
